﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace TiebaSpider.Beans
{
    public class HistoryForum : INotifyPropertyChanged
    {
        private string _name=String.Empty;
        public string Name 
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                EncodeName = System.Web.HttpUtility.UrlEncode(value);
                OnPropertyChanged(new PropertyChangedEventArgs("Name"));
            }
        }
        public string EncodeName { get; private set; }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }
    }

    public class ProxyHistoryForum
    {
        public ObservableCollection<HistoryForum> History { get; set; }
        public Setups.SetupControl setupController { private get; set; }

        public ProxyHistoryForum()
        {
            History = new ObservableCollection<HistoryForum>();
        }

        public ProxyHistoryForum(Setups.SetupControl controller)
            : this()
        {
            setupController = controller;
            refreshHistory();
        }

        private void refreshHistory()
        {
            if (setupController != null)
            {
                History.Clear();
                foreach (string item in setupController.UserLastBehavier.HistoryForum)
                {
                    HistoryForum historyItem = new HistoryForum();
                    historyItem.Name = item;
                    History.Add(historyItem);
                }
            }
        }

        public void AddHistory(string newForumName)
        {
            setupController.UserLastBehavier.HistoryForum.Add(newForumName);
            setupController.SaveBehaviors();
            refreshHistory();
        }

        public bool HasForum(string forumName)
        {
            bool result = false;
            foreach (HistoryForum item in History)
            {
                if (item.Name.Equals(forumName))
                {
                    result = true;
                    break;
                }
            }
            return result;
        }

        public HistoryForum GetForum(string forumName)
        {
            HistoryForum result = null;
            foreach (HistoryForum item in History)
            {
                if (item.Name.Equals(forumName))
                {
                    result = item;
                    break;
                }
            }
            return result;
        }
    }
}
